package com.fks.fks_service.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class PDFUtil {

    public static String savePDF(String fileUrl,String orderNo){
        String objectName = orderNo+".pdf";
        // 设置阿里云OSS的访问密钥ID和密钥Secret
        String accessKeyId = "LTAI4FvoZe4fFRwZsLHUQH4j";
        String accessKeySecret = "K7ygUSWvXfG7aH2pjVzRE96HleF7og";
        // 设置阿里云OSS的访问地址、存储桶名称和文件存储路径
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "htmlpdfbucket";

        try{
            URL url = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(5*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            //获取自己数组
            byte[] getData = readInputStream(inputStream);

            // 创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 上传PDF字节数组到阿里云OSS
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(getData));

            // 关闭OSSClient
            ossClient.shutdown();
            System.out.println("PDF文件上传成功！");

            if(inputStream!=null){
                inputStream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("PDF文件生成和上传失败！");
        }

        //获取访问地址
        try {
            // 创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 设置URL过期时间为1小时
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);

            // 生成预签名URL请求
            GeneratePresignedUrlRequest request2 = new GeneratePresignedUrlRequest(bucketName, objectName);
            request2.setExpiration(expiration);

            // 获取预签名URL
            URL url = ossClient.generatePresignedUrl(request2);

            System.out.println(String.valueOf(url));

            // 关闭OSSClient
            ossClient.shutdown();

            return String.valueOf(url);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取PDF文件URL失败！");
            return "";
        }
    }

    public static String getPDFUrl(String orderNo){
        String objectName = orderNo+".pdf";
        // 设置阿里云OSS的访问密钥ID和密钥Secret
        String accessKeyId = "LTAI4FvoZe4fFRwZsLHUQH4j";
        String accessKeySecret = "K7ygUSWvXfG7aH2pjVzRE96HleF7og";
        // 设置阿里云OSS的访问地址、存储桶名称和文件存储路径
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String bucketName = "htmlpdfbucket";
        //获取访问地址
        try {
            // 创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 设置URL过期时间为1小时
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);

            // 生成预签名URL请求
            GeneratePresignedUrlRequest request2 = new GeneratePresignedUrlRequest(bucketName, objectName);
            request2.setExpiration(expiration);

            // 获取预签名URL
            URL url = ossClient.generatePresignedUrl(request2);

            System.out.println(String.valueOf(url));

            // 关闭OSSClient
            ossClient.shutdown();

            return String.valueOf(url);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取PDF文件URL失败！");
            return "";
        }
    }


    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

}
